/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.oa.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.tgb.SpringActivemq.entity.StringData;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.oa.entity.OaNews;
import com.thinkgem.jeesite.modules.oa.service.OaNewsService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 新闻公告Controller
 * @author lgx
 * @version 2016-11-17
 */
@Controller
@RequestMapping(value = "${adminPath}/oa/oaNews")
public class OaNewsController extends BaseController {

    @Autowired
    private OaNewsService oaNewsService;

    @ModelAttribute
    public OaNews get(@RequestParam(required = false) String id) {
        OaNews entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = oaNewsService.get(id);
        }
        if (entity == null) {
            entity = new OaNews();
        }
        return entity;
    }

    @RequiresPermissions("oa:oaNews:view")
    @RequestMapping(value = {"list", ""})
    public String list(OaNews oaNews, HttpServletRequest request, HttpServletResponse response, Model model) {
        String companyid = UserUtils.getUser().getCompany().getId();
        oaNews.setCompanyId(companyid);
        oaNews.setCreateBy(UserUtils.getUser());
        int listAmount=0;
        Page<OaNews> page = oaNewsService.findMyWriteNews(new Page<OaNews>(request, response), oaNews);
        if(null!=page&&page.getList().size()>0){
            listAmount=page.getList().get(0).getCount();
        }
        model.addAttribute("listAmount",listAmount);
        model.addAttribute("page", page);
        return "modules/oa/oaNewsList";
    }

    /**
     * 上拉获取更多新闻公告
     * @param oaNews
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "getNoticeMore")
    @ResponseBody
    public List<Map> getNoticeMore(OaNews oaNews,@RequestParam(value="pageNo", required=true)int pageNo,@RequestParam(value="pageSize", required=true)int pageSize) {
        String companyId = UserUtils.getUser().getCompany().getId();
        oaNews.setCompanyId(companyId);
        oaNews.setCreateBy(UserUtils.getUser());
        Page<OaNews> page=new Page();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        List<Map> list=new ArrayList<>();
        list =oaNewsService.findTouchNews(page, oaNews, list);
        return list;
    }
    /**
     * 更多时查看
     *
     * @param oaNews
     * @param
     * @param
     * @param model
     * @return
     */

    @RequestMapping(value = "more")
        public String more(OaNews oaNews, HttpServletRequest request, HttpServletResponse response, Model model) {
            oaNews.setAuditFlag("1");
            oaNews.setSelf(true);
            Page<OaNews> page = oaNewsService.findPage(new Page<OaNews>(request, response), oaNews);
            model.addAttribute("page", page);
            return "modules/oa/oaNewsMore";


        }

    @RequiresPermissions("oa:oaNews:view")
    @RequestMapping(value = "form")
    public String form(OaNews oaNews, Model model) {
        if (oaNews == null || oaNews.getAuditFlag() == null) {
            oaNews.setAuditFlag("0");
        }
        if (oaNews == null || oaNews.getIsTopic() == null) {
            oaNews.setIsTopic("0");
        }


        model.addAttribute("oaNews", oaNews);
        return "modules/oa/oaNewsForm";
    }

    @RequiresPermissions("oa:oaNews:edit")
    @RequestMapping(value = "save")
    public String save(OaNews oaNews, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, oaNews)) {
            return form(oaNews, model);
        }

        String companyId = UserUtils.getUser().getCompany().getId();
        oaNews.setCompanyId(companyId);
        if( oaNews.getAuditMan().isEmpty()){
            oaNews.setAuditFlag("1");
            oaNewsService.save(oaNews);
        }else {
            oaNewsService.save(oaNews);
        }

        addMessage(redirectAttributes, "保存新闻公告成功");
        return "redirect:" + Global.getAdminPath() + "/oa/oaNews/?repage";
    }

    @RequiresPermissions("oa:oaNews:edit")
    @RequestMapping(value = "toUp")
    public String toUp(String id, String type, HttpServletRequest request, HttpServletResponse response, Model model) {
        OaNews oaNews = get(id);
        oaNews.setIsTopic(type);
        oaNewsService.save(oaNews);
        return "redirect:" + Global.getAdminPath() + "/oa/oaNews/?repage";
    }

    @RequiresPermissions("oa:oaNews:edit")
    @RequestMapping(value = "delete")
    public String delete(OaNews oaNews, RedirectAttributes redirectAttributes) {
        oaNewsService.delete(oaNews);
        addMessage(redirectAttributes, "删除新闻公告成功");
        return "redirect:" + Global.getAdminPath() + "/oa/oaNews/?repage";
    }

    /**
     * 根据ID获取审核新闻信息
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "getAuditNews")
    public String getAuditNews(String id, Model model) {
        OaNews oaNews = get(id);
        if(oaNews.getAuditMan()!=""){
        oaNews.setAuditManName(UserUtils.get(oaNews.getAuditMan()).getName());

        }
        oaNews.setCreateManName(UserUtils.get(oaNews.getCreateBy().getId()).getName());
        model.addAttribute("oaNews", oaNews);
        return "modules/oa/oaNewsView";
    }



    /**
     * 根据ID获取审核新闻信息
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "getMyAuditNews")
    public String getMyAuditNews(String id, Model model) {
        OaNews oaNews = get(id);
            if(oaNews.getAuditMan()!=""){

            oaNews.setAuditManName(UserUtils.get(oaNews.getAuditMan()).getName());
            }

        oaNews.setCreateManName(UserUtils.get(oaNews.getCreateBy().getId()).getName());
        model.addAttribute("oaNews", oaNews);
        return "modules/oa/myOaNewsView";
    }
    /**
     * @param id
     * @param auditFlag
     * @return
     */
    @RequestMapping(value = "auditNews")
    public String auditNews(String id, String auditFlag) {
        OaNews oaNews = get(id);
        oaNews.setAuditFlag(auditFlag);
        oaNewsService.update(oaNews);
        return "modules/sys/main1";
    }


    /**
     * 我的新闻列表
     */
    @RequiresPermissions("oa:oaNews:view")
    @RequestMapping(value = "self")
    public String selfList(OaNews oaNews, HttpServletRequest request, HttpServletResponse response, Model model) {
        oaNews.setAuditFlag("1");
        oaNews.setSelf(true);
        oaNews.setCompanyId(UserUtils.getUser().getCompany().getId());
        int listAmount=0;
        Page<OaNews> page = oaNewsService.findPage(new Page<OaNews>(request, response), oaNews);
        if(null!=page&&page.getList().size()>0){
            listAmount=page.getList().get(0).getCount();
        }
        model.addAttribute("listAmount",listAmount);
        model.addAttribute("page", page);
        return "modules/oa/myOaNewsList";
    }

    /**
     * 手机端新闻上拉加载功能
     * @param oaNews
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "getNewsMore")
    @ResponseBody
    public List<Map> getNewsMore(OaNews oaNews,@RequestParam(value="pageNo", required=true)int pageNo,@RequestParam(value="pageSize", required=true)int pageSize) {
        StringData resultData=new StringData();
        if(StringUtils.equals(oaNews.getAuditFlag(),"1")){
            oaNews.setSelf(true);
        }
        else if(StringUtils.equals(oaNews.getAuditFlag(),"0")){
            User user = UserUtils.getUser();
            oaNews.setAuditMan(user.getId());
        }
        Page<OaNews> page=new Page();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        List<Map> list=new ArrayList<>();
        list= oaNewsService.getNewsMore(page, oaNews, list);
        return list;
    }

    /**
     * OA手机端新闻公告
     */
    @RequiresPermissions("oa:oaNews:view")
    @RequestMapping(value = "selfNews")
    public String selfNews(OaNews oaNews, HttpServletRequest request, HttpServletResponse response, Model model) {
        oaNews.setAuditFlag("1");
        oaNews.setSelf(true);
        List<OaNews> list = oaNewsService.findList(oaNews);
        model.addAttribute("list", list);
        return "modules/oa/oaSelfNews";
    }

}